﻿using System.Collections.Specialized;
using System.Windows;
using Red.Core.GoogleAnalytics;
using Red.Launcher.ViewModels.Workspaces;

namespace Red.Launcher
{
    public partial class WorkspacesView
    {
        #region Private Types

        private struct AnalyticsData
        {
            public bool Initialised { get; set; }
        }

        #endregion Private Types

        #region Private Data

        private readonly string _analyticsCategory = typeof( WorkspacesView ).Name;
        private AnalyticsData _analytics = new AnalyticsData();

        #endregion Private Data

        #region C-Tor

        partial void InitializeAnalytics()
        {
            if ( !_analytics.Initialised )
            {
                _workspaces.Workspaces.CollectionChanged += Workspaces_CollectionChanged;

                _adminUsersButton.Click += ( object sender, RoutedEventArgs e ) => { GoogleAnalyticsService.Instance.SendEvent( "_adminUsersButton Clicked", _analyticsCategory ); };
                _adminWorkspacesButton.Click += ( object sender, RoutedEventArgs e ) => { GoogleAnalyticsService.Instance.SendEvent( "_adminWorkspacesButton Clicked", _analyticsCategory ); };
                _refreshWorkspacesButton.Click += ( object sender, RoutedEventArgs e ) => { GoogleAnalyticsService.Instance.SendEvent( "_refreshWorkspacesButton Clicked", _analyticsCategory ); };

                _analytics.Initialised = true;
            }
        }

        #endregion C-Tor

        #region Event Handlers

        private void Workspaces_CollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
        {
            if ( e.Action == NotifyCollectionChangedAction.Add )
            {
                foreach ( Workspace workspace in e.NewItems )
                {
                    workspace.OnActive += workspace_OnActive;
                }
            }
        }

        private void workspace_OnActive( Workspace workspace, bool isActive )
        {
            GoogleAnalyticsService.Instance.SendEvent( "Active Workspace Changed", _analyticsCategory, workspace.DisplayName );
        }

        #endregion Event Handlers
    }
}
